<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    fullscreen
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="分类" prop="typeLibId">
        <el-select v-model="dataForm.typeLibId">
          <el-option v-for="item in typeLibOptions" :key="item.id" :value="item.id" :label="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="代码" prop="code">
        <el-input v-model="dataForm.code" placeholder="代码"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>

      <el-form-item label="描述" prop="description">
        <el-input  type="textarea" v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>
      <template>
        <el-transfer v-model="dataForm.checkItemIds" :props="{key: 'id',label: 'name'}"
                     :titles="['检查项目', '已选检查项目']" :data="itemData"></el-transfer>
      </template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/def/template'
    import {fetchTypeLibList} from '@/api/def/typeLib'
    import {checkItemFetchItem} from '@/api/def/checkItem'

    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    id: 0,
                    code: '',
                    typeLibId: '',
                    name: '',
                    description: '',
                    checkItemIds:[]
                },
                typeLibOptions:[],
                dataRule: {
                    code: [
                        {required: true, message: '代码不能为空', trigger: 'blur'}
                    ],
                    typeLibId: [
                        {required: true, message: '分类ID不能为空', trigger: 'blur'}
                    ],

                    name: [
                        {required: true, message: '名称不能为空', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '描述不能为空', trigger: 'blur'}
                    ]
                },
                itemData:[]
            }
        },
        methods: {
            init(id) {
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        getObj(this.dataForm.id).then(response => {
                            this.dataForm = response.data.data
                        })
                    }
                })

                this.getTypeLibs()
                this.getItemData()
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        if (this.dataForm.id) {
                            console.log(this.dataForm)
                            putObj(this.dataForm).then(data => {
                                this.$message.success('修改成功')
                                this.visible = false
                                this.$emit('refreshDataList')
                            });
                        } else {
                            addObj(this.dataForm).then(data => {
                                this.$message.success('添加成功')
                                this.visible = false
                                this.$emit('refreshDataList')
                            })
                        }
                    }
                })
            },
            getTypeLibs(){
                fetchTypeLibList().then(result => {
                    this.typeLibOptions = result.data.data
                })
            },
            getItemData(){
                checkItemFetchItem().then(response => {
                    this.itemData = response.data.data;
                });
            }
        }
    }
</script>
